<template>
  <div>
    <div class="div">
      <span><img src="./购物车/箭头.png" alt="" @click="goback" /></span>
      <li>购物车</li>
    </div>

    <div class="box">
      <div class="block">
        <div class="shop" v-for="(item, index) in orderData" :key="index">
          <div class="line">
            <div class="pic" @click="selectShop(index)">
              <img
                :src="
                  item.isShop == 1
                    ? 'https://sucai.suoluomei.cn/sucai_zs/images/20191121093322-1.png'
                    : 'https://sucai.suoluomei.cn/sucai_zs/images/20191121093322-2.png'
                "
                alt
              />
            </div>
            <div>{{ item.shopName }}</div>
          </div>
          <div class="row" v-for="(row, i) in item.goodsList" :key="i">
            <div class="pic" @click="selectGoods(index, i)">
              <img
                :src="
                  row.isGoods == 1
                    ? 'http://sucai.suoluomei.cn/sucai_zs/images/20191121093322-1.png'
                    : 'https://sucai.suoluomei.cn/sucai_zs/images/20191121093322-2.png'
                "
                alt
              />
            </div>
            <div class="detail">
              <div class="photo">
                <img :src="row.defaultPic" alt />
              </div>
              <div class="info">
                <div class="title">{{ row.productName }}</div>
                <div class="norm">{{ row.format }}</div>
                <div class="rol">
                  <span class="amount">￥{{ row.price }}</span>
                  <van-stepper
                    v-model="row.count"
                    disable-input
                    @change="countNum()"
                  />
                  <div class="del" @click="delGoods(index, i)">
                    <img
                      src="https://sucai.suoluomei.cn/sucai_zs/images/20191121100950-3.png"
                      alt
                    />
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
        <div class="close">
          <div class="all" @click="selectAll()">
            <div class="pic">
              <img
                :src="
                  isAll == 1
                    ? 'https://sucai.suoluomei.cn/sucai_zs/images/20191121093322-1.png'
                    : 'https://sucai.suoluomei.cn/sucai_zs/images/20191121093322-2.png'
                "
                alt
              />
            </div>
            <span>全选</span>
          </div>
          <div class="refer">
            <div class="total">
              <span>合计:</span>
              <span>￥{{ allPrice }}</span>
            </div>
            <div class="settlement">结算</div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>      

<script>
import { shoopcar } from "@/api/shoopcar";
import { reactive,onMounted } from "vue";
export default {
  name: "IndexOrders",
  setup() {
    function goback() {
      history.go(-1);
    }

    let orderData = reactive({
      shoppingCartId: null,
      userId: null,
      supplyId: null,
      supply: {
        supplyId: null,
        classificationId: null,
        supplyName: "宠物零食",
        descrption: "小唐老板调香店",
        supplyPrice: 30,
        supplyImage: "http://43.136.137.117:5000/2.jpg",
      },
      user: {
        userId: 2,
        userPhone: null,
        password: null,
      },
      classification: {
        classificationId: null,
        userId: null,
        classificationName: "玩具类",
      },
    })

    shoopcar(orderData)
      .then((res) => {
        console.log(res);
        orderData = res
        console.log(orderData);
      })
      .catch(() => {

      });

    onMounted(() => {
      shoopcar();
    });

    
    return {
      goback,
    };
  },

  data() {
    return {
      isAll: 0,
      allPrice: 0,
      orderData: [
        {
          shopId: 1,
          shopName: "小唐老板",
          isShop: 0,
          goodsList: [
            {
              id: 1,
              shopId: 1,
              shopName: "小唐老板",
              isGoods: 0,
              defaultPic:
                "http://43.136.137.117:5000/2.jpg",
              productId: 1,
              productName: "小唐老板特地调制香 宠物沐浴露 明星狗同款（自营）",
              format: "香味分类",
              price: 20,
              count: 1,
            },
            {
              id: 2,
              shopId: 1,
              shopName: "小唐老板",
              isGoods: 0,
              defaultPic:
                "http://43.136.137.117:5000/3.jpg",
              productId: 2,
              productName: "小唐老板特地调制香 宠物沐浴露 明星狗同款（自营）",
              format: "狗粮类",
              price: 148,
              count: 1,
            },
          ],
        },
        {
          shopId: 2,
          shopName: "蓁蓁牛子店",
          isShop: 0,
          goodsList: [
            {
              id: 1,
              shopId: 2,
              shopName: "蓁蓁牛子店",
              isGoods: 0,
              defaultPic:
                "http://43.136.137.117:5000/5.jpg",
              productId: 1,
              productName: "蓁蓁牌狗粮 八重营养 高效吸收 由无畏代言的好狗粮",
              format: "玩具类",
              price: 181,
              count: 1,
            },
          ],
        },
        // {
        //   shopId: 3,
        //   shopName: "OPPO官方旗舰店",
        //   isShop: 0,
        //   goodsList: [
        //     {
        //       id: 1,
        //       shopId: 3,
        //       shopName: "OPPO官方旗舰店",
        //       isGoods: 0,
        //       defaultPic:
        //         "https://img10.360buyimg.com/cms/jfs/t6064/272/2163314583/157700/442d6477/593c1c49N7c63a7d9.jpg",
        //       productId: 1,
        //       productName: "OPPO R11 全网通",
        //       format: "蓝色30X",
        //       price: 1999,
        //       count: 1,
        //     },
        //     {
        //       id: 2,
        //       shopId: 3,
        //       shopName: "OPPO官方旗舰店",
        //       isGoods: 0,
        //       defaultPic:
        //         "https://img14.360buyimg.com/n0/jfs/t3142/194/4953241722/254855/1651c2b1/585b9021Nf653e48a.jpg",
        //       productId: 1,
        //       productName: "OPPO R9 全网通",
        //       format: "蓝色30X",
        //       price: 999,
        //       count: 1,
        //     },
        //   ],
        // },
      ],
    };
  },

  mounted() {},
  methods: {
    // 删除商品
    delGoods(index, i) {
      var list = this.orderData[index].goodsList;
      list.splice(i, 1);
      if (list.length == 0) {
        this.orderData.splice(index, 1);
      }
      this.watchAll();
      this.countPrice();
    },

    // 选择商品
    selectGoods(index, i) {
      var list = this.orderData[index].goodsList;
      if (list[i].isGoods == 0) {
        list[i].isGoods = 1;
      } else {
        list[i].isGoods = 0;
      }
      var anti = list.map((item) => item.isGoods).indexOf(0);
      if (anti == -1) {
        this.orderData[index].isShop = 1;
      } else {
        this.orderData[index].isShop = 0;
      }
      this.watchAll();
      this.countPrice();
    },

    // 选择店铺
    selectShop(index) {
      var list = this.orderData;
      var goodsList = list[index].goodsList;
      if (list[index].isShop == 0) {
        list[index].isShop = 1;
        for (let i in goodsList) {
          goodsList[i].isGoods = 1;
        }
      } else {
        list[index].isShop = 0;
        for (let i in goodsList) {
          goodsList[i].isGoods = 0;
        }
      }
      this.watchAll();
      this.countPrice();
    },

    // 全选
    selectAll() {
      var list = this.orderData;
      if (list.length == 0) {
        this.isAll = 0;
        return;
      }
      if (this.isAll == 0) {
        this.isAll = 1;
        for (let i in list) {
          list[i].isShop = 0;
          this.selectShop(i);
        }
      } else {
        this.isAll = 0;
        for (let i in list) {
          list[i].isShop = 1;
          this.selectShop(i);
        }
      }
      this.countPrice();
    },

    // 监听全选
    watchAll() {
      var list = this.orderData;
      if (list.length == 0) {
        this.isAll = 0;
        return;
      }
      var result = [];
      for (let i in list) {
        for (let j in list[i].goodsList) {
          result.push(list[i].goodsList[j].isGoods);
        }
      }
      var anti = result.map((item) => item).indexOf(0);
      if (anti == -1) {
        this.isAll = 1;
      } else {
        this.isAll = 0;
      }
    },

    // 更改商品数量
    countNum() {
      this.countPrice();
    },

    // 计算总价格
    countPrice() {
      let count = 0;
      var list = this.orderData;
      for (let i in list) {
        for (let j in list[i].goodsList) {
          if (list[i].goodsList[j].isGoods == 1) {
            count += list[i].goodsList[j].price * list[i].goodsList[j].count;
          }
        }
      }
      this.allPrice = count;
    },
  },
};
</script>

<style lang="less" scoped>
* {
  margin: 0;
  padding: 0;
}
.div {
  height: 5rem;
  background-color: rgb(185, 187, 223);
}
.div img {
  width: 1.25rem;
  height: 1.875rem;
}
.div > span {
  position: relative;
  font-size: 30px;
  top: 1.25rem;
  left: 0.625rem;
  color: aliceblue;
}
.div > li {
  list-style: none;
  font-size: 1.25rem;
  margin-left: 8.75rem;
  margin-top: -0.9375rem;
}

.box {
  position: relative;
  width: 100%;
  height: 100%;
  padding: 1.875rem;
  box-sizing: border-box;
  background: #fff;
  margin-bottom: 6.5rem;
}
.block {
  width: 100%;
}
.shop {
  margin-bottom: 3.75rem;
  box-shadow: 0px 8px 20px 0px rgba(97, 97, 97, 0.19);
  border-radius: 0.75rem;
  padding: 0.625rem;
  box-sizing: border-box;
  width: 21.6875rem;
  position: relative;
  left: -0.9375rem;
}

.line {
  display: flex;
  align-items: center;
}

.pic {
  width: 1.875rem;
  height: 1.875rem;
}

img {
  width: 100%;
  height: 80%;
  // object-fit: cover;
  object-fit: contain;
}

.line div:nth-child(2) {
  font-size: 0.625rem;
  margin-left: 1rem;
}

.row {
  display: flex;
  align-items: center;
  margin-top: -1.25rem;
}

.detail {
  width: 39.375rem;
  height: 14.75rem;

  margin-left: 0.625rem;
  padding: 1.25rem;
  box-sizing: border-box;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.photo {
  width: 5.125rem;
  height: 5.125rem;
}

.info {
  width: 74%;
  height: 100%;
  display: flex;
  justify-content: space-around;
  flex-direction: column;
  text-align: left;
  position: relative;
  left: 1.375rem;
}

.van-stepper {
  width: 7rem;
}

.norm {
  // font-size: 22px;
  color: #999999;
}

.rol {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.amount {
  // font-size: 38px;
  font-weight: 600;
  color: #ff3b30;
}

.del {
  width: 1.875rem;
  display: flex;
  align-items: center;
}

.close {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 3.125rem;
  border-top: 1px solid #eeeeee;
  background: #fff;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.all {
  display: flex;
  align-items: center;
  margin-left: 1.875rem;
}

.all span {
  color: #999999;
  // font-size: 24px;
  margin-left: 0.3125rem;
}

.refer {
  display: flex;
  align-items: center;
  height: 100%;
}

.total {
  display: flex;
  align-items: center;
}

.total span:nth-child(2) {
  // font-size: 38px;
  font-weight: 600;
  color: #ff3b30;
}

.settlement {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 6.25rem;
  height: 100%;
  background: #ff3b30;
  color: #fff;
  // font-size: 30px;
  margin-left: 0.9375rem;
}
</style>